package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.ProductSku;
import cn.wolfcode.shop.domain.ProductSkuProperty;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.ProductSkuPropertyMapper;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ProductSkuServiceImpl implements IProductSkuService {

    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    ProductSkuPropertyMapper productSkuPropertyMapper;


    @Override
    public void saveSku(GenerateSkuVo generateSkuVo) {
        //获取sku数据遍历
        for (ProductSku productSku : generateSkuVo.getProductSkuList()) {
            productSku.setProductId(generateSkuVo.getProduct().getId());
            productSkuMapper.insert(productSku);
            for (ProductSkuProperty productSkuProperty : productSku.getProductSkuPropertyList()) {
                productSkuProperty.setProductSkuId(productSku.getId());
                productSkuPropertyMapper.insert(productSkuProperty);
            }
        }
    }

    @Override
    public List<ProductSku> queryByProductId(Long id) {
        return productSkuMapper.queryByProductId(id);
    }

    @Override
    public ProductSku get(Long productSkuId) {
        return productSkuMapper.selectByPrimaryKey(productSkuId);
    }
}
